import { createRouter, createWebHistory } from 'vue-router'

const getPages = () => {
  const routes = import.meta.glob('../views/**/*.vue')
  const pages = Object.keys(routes).map((path) => {
    const name = path.match(/\.\/views\/(.*)\.vue$/)[1]
    let pathName = name.toLowerCase()
    if (name === 'index') {
      pathName = ''
    }
    return {
      path: `/${pathName}`,
      component: routes[path],
      name,
    }
  })
  return pages
}
const pages = getPages()
// 添加404错误页面
pages.push({
  path: '/:pathMatch(.*)*',
  component: () =>
    import(/* webpackChunkName: "404" */ '../views/error/404.vue'),
})
const router = createRouter({
  history: createWebHistory(),
  routes: pages,
})

router.beforeEach((to, from, next) => {
  next()
})
export default router
